package com.example.administrator.officeplatform.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.administrator.officeplatform.Audit.AuditActivity;
import com.example.administrator.officeplatform.Audit.ShowImageActivity;
import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.base.BaseFragment;
import com.example.administrator.officeplatform.kj.IpUtil;
import com.example.administrator.officeplatform.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;
import com.jauker.widget.BadgeView;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MessageFragment extends BaseFragment {
    private ImageView imageView_bc,imageView_dsh,im_fbc,im_zd,im_yxt,im_mfbc;
    private LinearLayout layout_cost,layout_travel,layout_reward,layout_borrowing,
            layout_dsh,layout_ysh,layout_mfq;
    private Banner banner;
    private BadgeView badgeView;
    private ImageView im_mbc;

    //设置图片资源:url或本地资源
    String[] images= new String[] {
            "http://218.192.170.132/BS80.jpg",
            "http://img.zcool.cn/community/0166c756e1427432f875520f7cc838.jpg",
            "http://img.zcool.cn/community/018fdb56e1428632f875520f7b67cb.jpg",
            "http://img.zcool.cn/community/01c8dc56e1428e6ac72531cbaa5f2c.jpg",
            "http://img.zcool.cn/community/01fda356640b706ac725b2c8b99b08.jpg",
            "http://img.zcool.cn/community/01fd2756e142716ac72531cbf8bbbf.jpg",
            "http://img.zcool.cn/community/0114a856640b6d32f87545731c076a.jpg"};

    //设置图片标题:自动对应
    String[] titles=new String[]{"十大星级品牌联盟，全场2折起","全场2折起","十大星级品牌联盟","嗨购5折不要停","12趁现在","嗨购5折不要停，12.12趁现在","实打实大顶顶顶顶"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);

    }

    @Override
    protected void initViews() {
//        banner = (Banner) findViewById(R.id.banner);
//        imageView_bc = (ImageView)findViewById(R.id.im_bc);
//        im_fbc = (ImageView)findViewById(R.id.im_fbc);
//        im_zd = (ImageView)findViewById(R.id.im_zd);
//        im_yxt = (ImageView)findViewById(R.id.im_yxt);
//        imageView_dsh = (ImageView)findViewById(R.id.im_mbc);
//        im_mfbc= (ImageView)findViewById(R.id.im_mfbc);

        layout_cost=(LinearLayout)findViewById(R.id.layout_cost);
        layout_travel=(LinearLayout)findViewById(R.id.layout_travel);
        layout_reward=(LinearLayout)findViewById(R.id.layout_reward);
        layout_borrowing=(LinearLayout)findViewById(R.id.layout_borrowing);
        layout_dsh=(LinearLayout)findViewById(R.id.layout_dsh);
        layout_ysh=(LinearLayout)findViewById(R.id.layout_ysh);
        layout_mfq=(LinearLayout)findViewById(R.id.layout_mfq);
        im_mbc=(ImageView) findViewById(R.id.im_mbc);


//        //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
//        //可选样式如下:
//        //1. Banner.CIRCLE_INDICATOR	显示圆形指示器
//        //2. Banner.NUM_INDICATOR	显示数字指示器
//        //3. Banner.NUM_INDICATOR_TITLE	显示数字指示器和标题
//        //4. Banner.CIRCLE_INDICATOR_TITLE	显示圆形指示器和标题
//        banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);
//
//        //设置轮播样式（没有标题默认为右边,有标题时默认左边）
//        //可选样式:
//        //Banner.LEFT	指示器居左
//        //Banner.CENTER	指示器居中
//        //Banner.RIGHT	指示器居右
//        banner.setIndicatorGravity(Banner.CENTER);
//
//        //设置轮播要显示的标题和图片对应（如果不传默认不显示标题）
//        banner.setBannerTitle(titles);
//
//        //设置是否自动轮播（不设置则默认自动）
//        banner.isAutoPlay(true)	;
//
//        //设置轮播图片间隔时间（不设置默认为2000）
//        banner.setDelayTime(5000);
//        //设置图片资源:可选图片网址/资源文件，默认用Glide加载,也可自定义图片的加载框架
//        //所有设置参数方法都放在此方法之前执行
//        //banner.setImages(images);
//
//        //自定义图片加载框架
//        banner.setImages(images, new Banner.OnLoadImageListener() {
//            @Override
//            public void OnLoadImage(ImageView view, Object url) {
//                System.out.println("加载中");
//                Glide.with(getContext()).load(url).into(view);
//                System.out.println("加载完");
//            }
//        });
//        //设置点击事件，下标是从1开始
//        banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {//设置点击事件
//            @Override
//            public void OnBannerClick(View view, int position) {
//                Toast.makeText(getContext(), "你点击了：" + position, Toast.LENGTH_LONG).show();
//
//            }
//        });
        getmessage();
    }

    @Override
    protected void initEvents() {
        layout_cost.setOnClickListener(new myOnClickListener());
        layout_travel.setOnClickListener(new myOnClickListener());
        layout_reward.setOnClickListener(new myOnClickListener());
        layout_borrowing.setOnClickListener(new myOnClickListener());
        layout_dsh.setOnClickListener(new myOnClickListener());
        layout_ysh.setOnClickListener(new myOnClickListener());
        layout_mfq.setOnClickListener(new myOnClickListener());
    }

    @Override
    protected void initData() {

    }

    class myOnClickListener implements View.OnClickListener {
        Intent intent = new Intent();

        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.layout_travel:
                    intent.setClass(getActivity(), TravelActivity.class);
                    startActivity(intent);
                    break;
                case R.id.layout_cost:
                    intent.setClass(getActivity(), CostActivity.class);
                    startActivity(intent);
                    break;
                case R.id.layout_dsh:
                    intent.setClass(getActivity(), AuditActivity.class);
//                    intent.setClass(getActivity(), ShowImageActivity.class);
                    intent.putExtra("type","1");//待审核
                    startActivityForResult(intent, 1);
                    break;
                case R.id.layout_ysh:
                    intent.setClass(getActivity(), AuditActivity.class);
                    intent.putExtra("type","2");//已审核
                    startActivityForResult(intent, 1);
                    break;
                case R.id.layout_reward:
                    intent.setClass(getActivity(), RewardActivity.class);
                    startActivity(intent);
                    break;
                case R.id.layout_borrowing:
                    intent.setClass(getActivity(), BorrowActivity.class);
                    startActivity(intent);
                    break;
                case R.id.layout_mfq:
                    intent.setClass(getActivity(), MyFQActivity.class);
                    startActivity(intent);
                    break;

            }

        }
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */

    private void getmessage() {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(IpUtil.getIP()+"/reimbursement/pageHome")
                .header("cookie", MyApplication.getMsession())
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //{"error":"您不属于事业部下,无法生成报销单号"}
                //{"number":"henfanachaojifana2017121900004"}
                final String str = response.body().string();
                final List<Integer> lsMap = JsonUtil.getPersons(str, new TypeToken<List<Integer>>() {
                }.getType());
                final Integer num = Integer.parseInt(lsMap.get(7).toString())-Integer.parseInt(lsMap.get(0).toString())-Integer.parseInt(lsMap.get(1).toString());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        badgeView = new BadgeView(getContext());
                        badgeView.setTargetView(im_mbc);
                        badgeView.setBadgeGravity(Gravity.TOP| Gravity.RIGHT);
                        badgeView.setBadgeCount(num);
                        badgeView.setTextSize(18);
                    }
                });


            }
        });
    }

//    //如果你需要考虑更好的体验，可以这么操作
//    @Override
//    public void onStart() {
//        super.onStart();
//        Log.i("--", "onStart");
//        banner.isAutoPlay(true);
//    }
//
//    @Override
//    public void onStop() {
//        super.onStop();
//        Log.i("--", "onStop");
//        banner.isAutoPlay(false);
//    }

}


